package com.sharkburner.security.service;

import java.util.Locale;

import javax.servlet.ServletRequestEvent;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.context.request.RequestContextListener;

import com.sharkburner.aeris.faces.common.i18n.LocaleBean;

public class CustomRequestContextListener extends RequestContextListener{
	private final String LOCALE_BEAN_ATTR = "localeBean"; 
	
	@Override
	public void requestInitialized(ServletRequestEvent requestEvent) {	
		super.requestInitialized(requestEvent);
		
		HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest();
		HttpSession session = request.getSession(false);	
		
		//TODO: eliminate this check; make sure the right url is coming from apache
		if(session != null){
			LocaleBean localeBean = (LocaleBean) session.getAttribute(LOCALE_BEAN_ATTR);
			if(localeBean != null){
				Locale locale = new Locale(localeBean.getLocale());
				LocaleContextHolder.setLocale(locale);
			}
		}
	}
}
